//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    title: {
      "name": "找票吧"
    },
    longitude: 113.324520,
    latitude: 23.099994,
    id:'',
    address:'',
    marketinfo: [{
        marketId:'',
      longitude: '',
      latitude: '',
      marketName: '',
      province: '',
      city: '',
      district: '',
      address: '',
      createUser: '',
     }],
    markers: [{
      id: 0,
      iconPath: "",
      latitude: 23.099994,
      longitude: 113.324520,
      width: 50,
      height: 50
    }]
  },
  onLoad: function () {
  
    // console.log("home:" + app.globalData.userInfo=='')
    // console.log("home:" + app.globalData.openid)
    if (app.globalData.userInfo == '' || app.globalData.userInfo ==null){
      this.getUserinfoTow();
    }
    if (app.globalData.marketinfo != null && app.globalData.marketinfo!=''){
      console.log("console.log()")
      this.remarkApp();
    }else{
      this.getLatLong();
    }
   
  },
  onShow: function () {
    this.setData({
      markers: [{
        latitude: this.data.latitude,
        longitude: this.data.longitude
      }]
    })
   
  },
  onReady: function () {

  },
  getLatLong:function(){
    var that = this;
    wx.getLocation({
      type: "wgs84",
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            latitude: res.latitude,
            longitude: res.longitude
          }]
        })
        that.addressLatLong();
      }
    })
  },
   addressLatLong: function(){
     var that=this
   var  qqmapsdk=  app.require();
     qqmapsdk.reverseGeocoder({//经纬度解析
       location: {
         latitude: that.data.latitude,
         longitude: that.data.longitude
       },
       success: function (res) {
         var marketinfo = that.data.marketinfo;
         var detailed = res.result.ad_info;
         var refFarea= res.result.address_reference.famous_area;
         if (refFarea==null){ refFarea= res.result.address_reference.landmark_l2}
         if (refFarea==null){ refFarea = res.result.address_reference.landmark_l1 }
           that.data.id=refFarea.id,
             marketinfo.marketId = refFarea.id,
             marketinfo.marketName = refFarea.title,
             marketinfo.latitude = refFarea.location.lat,
             marketinfo.longitude = refFarea.location.lng,
             marketinfo.province = detailed.province|| '',
             marketinfo.city = detailed.city|| '',
             marketinfo.district = detailed.district||'',
             marketinfo.detailed = res.result.address,
             marketinfo.address = res.result.address,
             that.data.latitude = refFarea.location.lat,
             that.data.longitude = refFarea.location.lng,
         that.setData({
           address: refFarea.title,
         })
       },
       fail: function (res) {
        // console.log(res);
       },
       complete: function (res) {
       //  console.log(res);
       }
     })
    },
  searchAddress:function(){
    wx.navigateTo({
      url: '/pages/ticket/address/address'　　// 页面 地址
    })
  },
  userInfo:function(){
    this.appAddress();
    wx.switchTab({
      url: '/pages/ticket/personal/personal'    //我的
    });  
  },

  ticket:function(){
    app.globalData.vtype = 0;
    this.appAddress();
    wx.navigateTo({
      url: "/pages/ticket/Vticket/index"　　// 页面 小票
    })
     
  },
  park:function(){
    app.globalData.vtype=1;
    this.appAddress();
    wx.navigateTo({
      url: "/pages/ticket/Vticket/index" ,　　// 页面 停车
    })
  },

  remarkApp: function () {//第二次進入 不要再加載 进入程序 调用此方法
  // debugger
    // var that = this.data
    // that.marketinfo = app.globalData.marketinfo 
    //    that.id = app.globalData.id
    //    that.latitude = app.globalData.latitude
    //    that.longitude = app.globalData.longitude
    //    that.address = app.globalData.address 
       this.setData({
       marketinfo : app.globalData.marketinfo,
       id         : app.globalData.id,
       latitude   : app.globalData.latitude,
       longitude  : app.globalData.longitude,
       address    : app.globalData.address ,
       })
  },

  appAddress:function(){//第一次進入 /进入程序 调用此方法
    var that=this.data
    app.globalData.marketinfo = that.marketinfo
    app.globalData.id = that.id
    app.globalData.latitude = that.latitude
    app.globalData.longitude = that.longitude
    app.globalData.address = that.address
  },

    getUserinfoTow:function(){
      var appthat = app.globalData
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                appthat.userInfo = res.userInfo
                app.requsetLogin("User/login", appthat.openid, appthat.userInfo)
              }
            })
          }
        }
      })
    },
})